function [rep,url,filekey]=move2s3db(x)
%function [rep,url]=move2s3db(x) accepts a file, a url and a key and submits a
%file into s3db folder by breaking it into fragments
%input: x is a struct with fields url, key and file.If it contains
%project_id, rule_id and resource_id, will run the query for insert file
%output: report given by s3db on each fragment entry
if ~isfield(x,'size')
    x.size=5000;
end

if size(fields(x),1)<3
    disp 'x must contain at least file, url and key. size is optional';
    return;
end
disp 'Reading the file to strings';
strings = build64enc_string(x.file, x.size);

tot = size(strings, 2);
%ge thte size of the file
filesize=dir(x.file);
filesize=filesize.bytes;

url=cell(tot,1);
rep=cell(tot,1);

%let s3db know that there is a file coming
if ~(x.url(end)=='/')
    x.url = [x.url, '/'];
end
init = [x.url,'uploads.php?key=',x.key, '&filename=',x.file,'&filesize=',num2str(filesize),'&format=xml'];
fkre=urlread(init);
%take in the filekey
[a,b,c,d]=regexp(fkre, '<filekey>[a-zA-Z0-9]*</filekey>');
filekey=fkre(strfind(fkre,'<filekey>')+9:strfind(fkre,'</filekey>')-1);
if isempty(filekey)
    disp 'a filekey was not provided. Here is the S3DB report:'
    disp(fkre);
    return;
end

for i=1:tot
    disp(['sending frag ',num2str(i)]);
    url{i} = [x.url,'uploads.php'];%?filekey=',filekey,'&fragNr=',num2str(i),'/',num2str(tot),'&fileStr=',strings{i},'&encode=2'];
    
    try
       %rep{i}=urlread(url{i}, 'GET', {'fileStr',strings{i}});
       rep{i}=urlread(url{i}, 'POST', {'filekey', filekey, 'fragNr', [num2str(i),'/',num2str(tot)], 'fileStr',strings{i}, 'encode', '2'});
    catch
       disp(['Frag ',num2str(i),' was not inserted 1x. Trying again']);
       try
            rep{i}=urlread(url{i}, 'POST', {'filekey', filekey, 'fragNr', [num2str(i),'/',num2str(tot)], 'fileStr',strings{i}, 'encode', '2'});
       catch
            disp(['Frag ',num2str(i),' was not inserted 2x. Trying again']);
            try
               rep{i}=urlread(url{i}, 'POST', {'filekey', filekey, 'fragNr', [num2str(i),'/',num2str(tot)], 'fileStr',strings{i}, 'encode', '2'});
                %rep{i}=urlread(url{i});
            catch
                disp(['Frag ',num2str(i),' was not inserted 3x. Trying again']);
                try
                    %rep{i}=urlread(url{i}, 'GET', {'fileStr',strings{i}});
                   rep{i}=urlread(url{i}, 'POST', {'filekey', filekey, 'fragNr', [num2str(i),'/',num2str(tot)], 'fileStr',strings{i}, 'encode', '2'});
                catch
                    disp(['Frag ',num2str(i),' could not be inserted. Giving up :-(']);
                    return;
                end
            end
       end
    end
end
            
      
if (isfield(x,'rule_id'))
    s3ql.url = x.url;
    s3ql.key = x.key;
    s3ql.insert = 'file';
    
   
    s3ql.where.filekey = filekey;
    s3ql.where.rule_id = x.rule_id;
    if(isfield(x,'item_id'))
    s3ql.where.item_id = x.item_id;
    elseif(isfield(x,'instance_id'))
    s3ql.where.item_id = x.instance_id;
    end
    
    
    try 
        url{tot+1} = S3QLSyntax(s3ql);
        pause(0.001);
        rep{tot+1} = urlread(url{tot+1});
    catch 
        disp 'Could not put file in s3db';
    end
end

function strings = build64enc_string(file, x)

%builstring converts a file to encoded strings with size x
%syntax: strings = buildstring(file, x)
%file is the path of the file to be converted to strings
%x is the size of the fragments

%Helena F Deus, February 1, 2007

if (nargin<2 | ~isnumeric(x)) x = 5000; 
end

fid=fopen(file,'r');
i=1;
while ~feof(fid)
    %unfriendly = {'\n', '+'};
    %replace = {'', '%2b'};
   % y{i}= regexprep(base64encode(fread(fid, x)), unfriendly, replace);
    
    y{i}= base64encode(fread(fid, x));
    %disp(['encoding frag', num2str(i)]);
    i=i+1;
  
end
fclose(fid);
%y=strcat(y{:});
strings =y;
